package cn.exrick.xboot.modules.base.utils;


import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.vo.Result;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalAdjusters;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 日期工具类
 */
public class DateUtils {

    /**
     * 自定义格式化
     */
    public static final String YEAR_MONTH_FORMATTER = "yyyy-MM";
    public static final String DATE_FORMATTER = "yyyy-MM-dd";
    public static final String DATETIME_FORMATTER = "yyyy-MM-dd HH:mm:ss";
    public static final String TIME_FORMATTER = "HH:mm:ss";
    public static final String YEAR_MONTH_FORMATTER_SHORT = "yyyyMM";
    public static final String DATE_FORMATTER_SHORT = "yyyyMMdd";
    public static final String DATETIME_FORMATTER_SHORT = "yyyyMMddHHmmss";
    public static final String TIME_FORMATTER_SHORT = "HHmmss";
    /**
     * 24小时时间正则表达式
     */
    public static final String MATCH_TIME_24 = "(([0-1][0-9])|2[0-3]):[0-5][0-9]:[0-5][0-9]";
    /**
     * 日期正则表达式
     */
    public static final String REGEX_DATA = "^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))";


    /**
     * 获取当前时间戳
     *
     * @return timestamp
     */
    public static long getTimestamp() {
        // LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
        // LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
        // Timestamp.valueOf(LocalDateTime.now()).getTime();
        // Instant.now().toEpochMilli();
        return Instant.now().toEpochMilli();
    }

    /**
     * 获取当前年份
     *
     * @return yyyy
     */
    public static int getCurrentYear() {
        return LocalDate.now().getYear();
    }

    /**
     * 获取当前年月
     *
     * @return yyyy-MM
     */
    public static String getCurrentYearMonth() {
        return getCurrentDate(YEAR_MONTH_FORMATTER);
    }

    /**
     * 获取当前日期
     *
     * @return yyyy-MM-dd
     */
    public static String getCurrentDate() {
        return getCurrentDate(DATE_FORMATTER);
    }

    /**
     * 获取下一天日期
     *
     * @return yyyy-MM-dd
     */
    public static String getNextDate() {
        return getNextDate(DATE_FORMATTER);
    }

    /**
     * 获取当前时间
     *
     * @return HHmmss
     */
    public static String getCurrentTime() {
        return getCurrentTime(TIME_FORMATTER);
    }

    /**
     * 获取当前日期时间
     *
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String getCurrentDateTime() {
        return getCurrentDateTime(DATETIME_FORMATTER);
    }

    /**
     * 获取当前年月
     *
     * @return yyyyMM
     */
    public static String getCurrentYearMonthShort() {
        return getCurrentDate(YEAR_MONTH_FORMATTER_SHORT);
    }

    /**
     * 获取当前日期
     *
     * @return yyyyMMdd
     */
    public static String getCurrentDateShort() {
        return getCurrentDate(DATE_FORMATTER_SHORT);
    }

    /**
     * 获取前一天日期
     *
     * @return yyyy-MM-dd
     */
    public static String getfrontDateShort() {
        return getFrontDate(DATE_FORMATTER_SHORT);
    }

    /**
     * 获取下一天日期
     *
     * @return yyyy-MM-dd
     */
    public static String getNextDateShort() {
        return getNextDate(DATE_FORMATTER_SHORT);
    }

    /**
     * 获取当前时间
     *
     * @return HHmmss
     */
    public static String getCurrentTimeShort() {
        return getCurrentTime(TIME_FORMATTER_SHORT);
    }

    /**
     * 获取当前日期时间
     *
     * @return yyyyMMddHHmmss
     */
    public static String getCurrentDateTimeShort() {
        return getCurrentDateTime(DATETIME_FORMATTER_SHORT);
    }

    /**
     * 获取当前日期
     *
     * @param pattern 格式化
     */
    public static String getCurrentDate(String pattern) {
        return LocalDate.now().format(DateTimeFormatter.ofPattern(pattern));
    }

    /**
     * 获取前一天日期
     *
     * @param pattern
     * @return
     */
    public static String getFrontDate(String pattern) {
        return LocalDate.now().plusDays(-1).format(DateTimeFormatter.ofPattern(pattern));
    }

    /**
     * 获取下一天日期
     *
     * @param pattern 格式化
     */
    public static String getNextDate(String pattern) {
        return LocalDate.now().plusDays(1).format(DateTimeFormatter.ofPattern(pattern));
    }

    /**
     * 获取当前时间
     *
     * @param pattern 格式化
     */
    public static String getCurrentTime(String pattern) {
        return LocalTime.now().format(DateTimeFormatter.ofPattern(pattern));
    }

    /**
     * 获取当前日期时间
     *
     * @param pattern 格式化
     */
    public static String getCurrentDateTime(String pattern) {
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(pattern));
    }

    /**
     * 时间戳转日期
     *
     * @param timestamp 时间戳
     */
    public static LocalDateTime timestampToLocalDateTime(long timestamp) {
        Instant instant = Instant.ofEpochMilli(timestamp);
        return LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
    }

    /**
     * 日期转时间戳
     *
     * @param localDateTime 日期
     */
    public static long localDateTimeToTimestamp(LocalDateTime localDateTime) {
        return localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

    /**
     * 时间戳转日期时间
     *
     * @param timestamp 时间戳
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String formatTimestamp(long timestamp) {
        return formatTimestamp(timestamp, DATETIME_FORMATTER);
    }

    /**
     * 时间戳转日期时间
     *
     * @param timestamp 时间戳
     * @return yyyyMMddHHmmss
     */
    public static String formatTimestampShort(long timestamp) {
        return formatTimestamp(timestamp, DATETIME_FORMATTER_SHORT);
    }

    /**
     * 时间戳转日期
     *
     * @param timestamp 时间戳
     * @param pattern   格式化
     */
    public static String formatTimestamp(long timestamp, String pattern) {
        Instant instant = Instant.ofEpochMilli(timestamp);
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
        return formatLocalDateTime(localDateTime, pattern);
    }

    /**
     * 日期格式化字符串
     *
     * @param localDate 日期
     * @return yyyy-MM-dd
     */
    public static String formatLocalDate(LocalDate localDate) {
        return formatLocalDate(localDate, DATE_FORMATTER);
    }

    /**
     * 日期格式化字符串
     *
     * @param localDate 日期
     * @return yyyyMMdd
     */
    public static String formatLocalDateShort(LocalDate localDate) {
        return formatLocalDate(localDate, DATE_FORMATTER_SHORT);
    }

    /**
     * 时间格式化字符串
     *
     * @param localTime 时间
     * @return HH:mm:ss
     */
    public static String formatLocalTime(LocalTime localTime) {
        return formatLocalTime(localTime, TIME_FORMATTER);
    }

    /**
     * 时间格式化字符串
     *
     * @param localTime 时间
     * @return HHmmss
     */
    public static String formatLocalTimeShort(LocalTime localTime) {
        return formatLocalTime(localTime, TIME_FORMATTER_SHORT);
    }

    /**
     * 日期时间格式化字符串
     *
     * @param localDateTime 日期时间
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String formatLocalDateTime(LocalDateTime localDateTime) {
        return formatLocalDateTime(localDateTime, DATETIME_FORMATTER);
    }

    /**
     * 日期时间格式化字符串
     *
     * @param localDateTime 日期时间
     * @return yyyyMMddHHmmss
     */
    public static String formatLocalDateTimeShort(LocalDateTime localDateTime) {
        return formatLocalDateTime(localDateTime, DATETIME_FORMATTER_SHORT);
    }

    /**
     * 日期格式化字符串
     *
     * @param localDate 日期
     * @param pattern   格式化
     */
    public static String formatLocalDate(LocalDate localDate, String pattern) {
        return localDate.format(DateTimeFormatter.ofPattern(pattern));
    }

    /**
     * 时间格式化字符串
     *
     * @param localTime 时间
     * @param pattern   格式化
     */
    public static String formatLocalTime(LocalTime localTime, String pattern) {
        return localTime.format(DateTimeFormatter.ofPattern(pattern));
    }

    /**
     * 日期时间格式化字符串
     *
     * @param localDateTime 日期时间
     * @param pattern       格式化
     */
    public static String formatLocalDateTime(LocalDateTime localDateTime, String pattern) {
        return localDateTime.format(DateTimeFormatter.ofPattern(pattern));
    }

    /**
     * 日期字符串转日期
     *
     * @param date    日期字符串
     * @param pattern 格式化
     */
    public static LocalDate parseLocalDate(String date, String pattern) {
        return LocalDate.parse(date, DateTimeFormatter.ofPattern(pattern));
    }

    /**
     * 时间字符串转时间
     *
     * @param time    时间字符串
     * @param pattern 格式化
     */
    public static LocalTime parseLocalTime(String time, String pattern) {
        return LocalTime.parse(time, DateTimeFormatter.ofPattern(pattern));
    }

    /**
     * 日期时间字符串转日期时间
     *
     * @param dateTime 日期时间字符串
     * @param pattern  格式化
     */
    public static LocalDateTime parseLocalDateTime(String dateTime, String pattern) {
        return LocalDateTime.parse(dateTime, DateTimeFormatter.ofPattern(pattern));
    }

    /**
     * 获取本周第一天
     */
    public static LocalDate getCurrentWeekFirstDate() {
        return LocalDate.now().minusWeeks(0).with(DayOfWeek.MONDAY);
    }

    /**
     * 获取本周最后一天
     */
    public static LocalDate getCurrentWeekLastDate() {
        return LocalDate.now().minusWeeks(0).with(DayOfWeek.SUNDAY);
    }

    /**
     * 获取本月第一天
     */
    public static LocalDate getCurrentMonthFirstDate() {
        return LocalDate.now().with(TemporalAdjusters.firstDayOfMonth());
    }

    /**
     * 获取本月最后一天
     */
    public static LocalDate getCurrentMonthLastDate() {
        return LocalDate.now().with(TemporalAdjusters.lastDayOfMonth());
    }

    /**
     * 获取指定周第一天
     *
     * @param date    日期字符串
     * @param pattern 格式化
     */
    public static LocalDate getWeekFirstDate(String date, String pattern) {
        return parseLocalDate(date, pattern).minusWeeks(0).with(DayOfWeek.MONDAY);
    }

    /**
     * 获取指定周第一天
     *
     * @param localDate 日期
     */
    public static LocalDate getWeekFirstDate(LocalDate localDate) {
        return localDate.minusWeeks(0).with(DayOfWeek.MONDAY);
    }

    /**
     * 获取指定周最后一天
     *
     * @param date    日期字符串
     * @param pattern 格式化
     */
    public static LocalDate getWeekLastDate(String date, String pattern) {
        return parseLocalDate(date, pattern).minusWeeks(0).with(DayOfWeek.SUNDAY);
    }

    /**
     * 获取指定周最后一天
     *
     * @param localDate 日期
     */
    public static LocalDate getWeekLastDate(LocalDate localDate) {
        return localDate.minusWeeks(0).with(DayOfWeek.SUNDAY);
    }


    /**
     * 获取指定月份月第一天
     *
     * @param date    日期字符串
     * @param pattern 格式化
     */
    public static LocalDate getMonthFirstDate(String date, String pattern) {
        return parseLocalDate(date, pattern).with(TemporalAdjusters.firstDayOfMonth());
    }
    public static LocalDate getMonthLastDate(String date, String pattern) {
        return parseLocalDate(date, pattern).with(TemporalAdjusters.lastDayOfMonth());
    }


    /**
     * 获取指定月份月第一天
     *
     * @param localDate
     */
    public static LocalDate getMonthFirstDate(LocalDate localDate) {
        return localDate.with(TemporalAdjusters.firstDayOfMonth());
    }

    /**
     * 获取当前星期
     *
     * @return 1:星期一、2:星期二、3:星期三、4:星期四、5:星期五、6:星期六、7:星期日
     */
    public static int getCurrentWeek() {
        return LocalDate.now().getDayOfWeek().getValue();
    }


    /**
     * 获取当前星期
     *
     * @param localDate 日期
     */
    public static int getWeek(LocalDate localDate) {
        return localDate.getDayOfWeek().getValue();
    }

    /**
     * 获取指定日期的星期
     *
     * @param date    日期字符串
     * @param pattern 格式化
     */
    public static int getWeek(String date, String pattern) {
        return parseLocalDate(date, pattern).getDayOfWeek().getValue();
    }

    /**
     * 日期相隔天数
     *
     * @param startLocalDate 起日期
     * @param endLocalDate   止日期
     */
    public static long intervalDays(LocalDate startLocalDate, LocalDate endLocalDate) {
        return endLocalDate.toEpochDay() - startLocalDate.toEpochDay();
    }

    /**
     * 日期距今天多少天
     *
     * @param localDateTime 起日期
     */
    public static int intervalDays(LocalDateTime localDateTime) {
        LocalDate localDate = LocalDate.now();
        return localDate.compareTo(localDateTime.toLocalDate());
    }

    /**
     * 日期相隔小时
     *
     * @param startLocalDateTime 起日期时间
     * @param endLocalDateTime   止日期时间
     */
    public static long intervalHours(LocalDateTime startLocalDateTime, LocalDateTime endLocalDateTime) {
        return Duration.between(startLocalDateTime, endLocalDateTime).toHours();
    }

    /**
     * 日期相隔分钟
     *
     * @param startLocalDateTime 起日期时间
     * @param endLocalDateTime   止日期时间
     * @return
     */
    public static long intervalMinutes(LocalDateTime startLocalDateTime, LocalDateTime endLocalDateTime) {
        return Duration.between(startLocalDateTime, endLocalDateTime).toMinutes();
    }

    /**
     * 日期相隔毫秒数
     *
     * @param startLocalDateTime 起日期时间
     * @param endLocalDateTime   止日期时间
     */
    public static long intervalMillis(LocalDateTime startLocalDateTime, LocalDateTime endLocalDateTime) {
        return Duration.between(startLocalDateTime, endLocalDateTime).toMillis();
    }

    /**
     * 当前是否闰年
     */
    public static boolean isCurrentLeapYear() {
        return LocalDate.now().isLeapYear();
    }

    /**
     * 是否闰年
     */
    public static boolean isLeapYear(LocalDate localDate) {
        return localDate.isLeapYear();
    }

    /**
     * 是否当天
     *
     * @param localDate 日期
     */
    public static boolean isToday(LocalDate localDate) {
        return LocalDate.now().equals(localDate);
    }

    /**
     * 获取此日期时间与默认时区<Asia/Shanghai>组合的时间毫秒数
     *
     * @param localDateTime 日期时间
     */
    public static Long toEpochMilli(LocalDateTime localDateTime) {
        return localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

    /**
     * 获取此日期时间与指定时区组合的时间毫秒数
     *
     * @param localDateTime 日期时间
     */
    public static Long toSelectEpochMilli(LocalDateTime localDateTime, ZoneId zoneId) {
        return localDateTime.atZone(zoneId).toInstant().toEpochMilli();
    }

    /**
     * 计算距今天指定天数的日期
     *
     * @param days
     * @return
     */
    public static String getDateAfterDays(int days) {
        Calendar date = Calendar.getInstance();// today
        date.add(Calendar.DATE, days);
        SimpleDateFormat simpleDate = new SimpleDateFormat(DATE_FORMATTER);
        return simpleDate.format(date.getTime());
    }

    /**
     * 在指定的日期的前几天或后几天
     *
     * @param source 源日期(yyyy-MM-dd)
     * @param days   指定的天数,正负皆可
     * @return
     * @throws ParseException
     */
    public static String addDays(String source, int days) {
        Date date = localDateToDate(parseLocalDate(source, DATE_FORMATTER));
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, days);
        SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMATTER);
        return dateFormat.format(calendar.getTime());
    }

    /**
     * LocalDate转换成Date
     *
     * @param localDate
     * @return
     */
    public static Date localDateToDate(LocalDate localDate) {
        ZoneId zone = ZoneId.systemDefault();
        Instant instant = localDate.atStartOfDay().atZone(zone).toInstant();
        return Date.from(instant);
    }

    /**
     * 24小时时间校验
     *
     * @param time
     * @return
     */
    public static boolean isValidate24(String time) {
        Pattern p = Pattern.compile(MATCH_TIME_24);
        return p.matcher(time).matches();
    }

    /**
     * 日期校验
     *
     * @param date
     * @return
     */
    public static boolean isDate(String date) {
        Pattern pat = Pattern.compile(REGEX_DATA);
        Matcher mat = pat.matcher(date);
        return mat.matches();
    }

    /**
     * 判断当前时间是否在指定时间范围
     *
     * @param from 开始时间
     * @param to   结束时间
     * @return 结果
     */
    public static boolean between(LocalTime from, LocalTime to) {
        LocalTime now = LocalTime.now();
        return now.isAfter(from) && now.isBefore(to);
    }

    /**
     * 判断指定时间是属于上午还是下午
     *
     * @return 结果
     */
    public static String morning_or_afternoon(LocalDateTime now) {
        //        LocalDateTime now = LocalDateTime.now();
        switch (now.get(ChronoField.AMPM_OF_DAY)) {
            case 0:
                if (now.getHour() > 6) {
                    return "上午";
                } else {
                    return "凌晨";
                }
            case 1:
                if (now.getHour() > 18) {
                    return "晚上";
                } else {
                    return "下午";
                }
        }
        return "";
    }

    /**
     * 根据当前时间 返回上午或者下午小时
     */
    public static String morningToAfternoon(LocalDateTime now) {
        //        LocalDateTime now = LocalDateTime.now();
        String result = "";
        switch (now.get(ChronoField.AMPM_OF_DAY)) {
            case 0:
                if (now.getHour() > 6) {
                    result = "上午 " + now.format(DateTimeFormatter.ofPattern("HH:mm"));
                    return result;
                } else {
                    result = "凌晨 " + now.format(DateTimeFormatter.ofPattern("HH:mm"));
                    return result;
                }
            case 1:
                if (now.getHour() > 18) {
                    result = "晚上 " + now.format(DateTimeFormatter.ofPattern("HH:mm"));
                    return result;
                } else {
                    result = "下午 " + now.format(DateTimeFormatter.ofPattern("HH:mm"));
                    return result;
                }
        }
        return result;
    }

    /**
     * 将某时间字符串转为自定义时间格式的LocalDateTime
     *
     * @param time
     * @param format
     * @return
     */
    public static LocalDateTime parseStringToDateTime(String time, String format) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
        return LocalDateTime.parse(time, df);
    }

    public static LocalDateTime parseStringToDateTime(String time) {
        return parseStringToDateTime(time, DATETIME_FORMATTER);
    }

    public static String getDatePoor(Date endDate, Date nowDate) {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        long ns = 1000;
// 获得两个时间的毫秒时间差异
        long time = endDate.getTime() - nowDate.getTime();
// 计算差多少天
        long day = time / nd;
// 计算差多少小时
        long hour = time % nd / nh;
// 计算差多少分钟
        long min = time % nd % nh / nm;
// 计算差多少秒//输出结果
        long sec = time % nd % nh % nm / ns;

        if (day > 0){
            return day + "天" + hour + "小时" + min + "分钟";
        }else if (hour > 0){
            return hour + "小时" + min + "分钟";
        }else if (min > 0){
            return min + "分钟";
        }else {
            return sec + "秒";
        }

    }
    public static Map<String,Object> getDatePoorMap(Date endDate, Date nowDate) {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        long ns = 1000;
// 获得两个时间的毫秒时间差异
        long time = endDate.getTime() - nowDate.getTime();
// 计算差多少天
        long day = time / nd;
// 计算差多少小时
        long hour = time % nd / nh;
// 计算差多少分钟
        long min = time % nd % nh / nm;
// 计算差多少秒//输出结果
        long sec = time % nd % nh % nm / ns;
        Map<String,Object> resultTime = new HashMap<>();
        if (day >= 2){
            resultTime.put("elapsedTimeStatus",1);
            resultTime.put("elapsedTime",day + "天" + hour + "小时" + min + "分钟");
            return resultTime;
        }else if (day > 0){
            resultTime.put("elapsedTimeStatus",0);
            resultTime.put("elapsedTime",day + "天" + hour + "小时" + min + "分钟");
            return resultTime;
        }else if (hour > 0){
            resultTime.put("elapsedTimeStatus",0);
            resultTime.put("elapsedTime",hour + "小时" + min + "分钟");
            return resultTime;
        }else if (min > 0){
            resultTime.put("elapsedTimeStatus",0);
            resultTime.put("elapsedTime",min + "分钟"+ sec +"秒");
            return resultTime;
        }else {
            resultTime.put("elapsedTimeStatus",0);
            resultTime.put("elapsedTime",sec + "秒");
            return resultTime;
        }

    }
    public static Map<String,Object> getDateTimePoor(LocalDateTime endDateTime, LocalDateTime nowDateTime) {
        Date endDate = Date.from(endDateTime.atZone(ZoneId.systemDefault()).toInstant());
        Date nowDate = Date.from(nowDateTime.atZone(ZoneId.systemDefault()).toInstant());
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        long ns = 1000;
// 获得两个时间的毫秒时间差异
        long time = endDate.getTime() - nowDate.getTime();
// 计算差多少天
        long day = time / nd;
// 计算差多少小时
        long hour = time % nd / nh;
// 计算差多少分钟
        long min = time % nd % nh / nm;
// 计算差多少秒//输出结果
        long sec = time % nd % nh % nm / ns;

        Map<String,Object> resultTime = new HashMap<>();
        if (day >= 2){
            resultTime.put("elapsedTimeStatus",1);
            resultTime.put("elapsedTime",day + "天" + hour + "小时" + min + "分钟");
            return resultTime;
        }else if (day > 0){
            resultTime.put("elapsedTimeStatus",0);
            resultTime.put("elapsedTime",day + "天" + hour + "小时" + min + "分钟");
            return resultTime;
        }else if (hour > 0){
            resultTime.put("elapsedTimeStatus",0);
            resultTime.put("elapsedTime",hour + "小时" + min + "分钟");
            return resultTime;
        }else if (min > 0){
            resultTime.put("elapsedTimeStatus",0);
            resultTime.put("elapsedTime",min + "分钟"+ sec +"秒");
            return resultTime;
        }else {
            resultTime.put("elapsedTimeStatus",0);
            resultTime.put("elapsedTime",sec + "秒");
            return resultTime;
        }

    }



    public static int findLeastNumOfUniqueInts(int[] arr, int k) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int num : arr) {
            map.put(num, map.getOrDefault(num, 0) + 1);
        }

        List<Integer> valuesList = new ArrayList<>(map.values());
        valuesList.sort((o1, o2) -> o1 - o2);

        int count = valuesList.size();
        for (int i = 0; i < valuesList.size(); i++) {
            if (k >= valuesList.get(i)) {
                k -= valuesList.get(i);
                count--;
            } else {
                return count;
            }
        }

        return count;
    }

    public static void merge(int[] nums1, int m, int[] nums2, int n) {
        for (int i = 0; i < nums1.length; i++) {

        }
    }

    public static Map<String,String> getBeforeMonth(){
        Map<String,String> month = new HashMap<>();
        LocalDate currentDate = LocalDate.now();

        // 获取上个月的第一天
        LocalDate firstDayOfLastMonth = currentDate.minusMonths(1).with(TemporalAdjusters.firstDayOfMonth());

        // 获取上个月的最后一天
        LocalDate lastDayOfLastMonth = currentDate.minusMonths(1).with(TemporalAdjusters.lastDayOfMonth());
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String firstDay = firstDayOfLastMonth.format(formatter);
        String lastDay = lastDayOfLastMonth.format(formatter);
        month.put("firstDay",firstDay);
        month.put("lastDay",lastDay);

        return month;
    }

    public static Integer getBeforeMonthNum(){
        LocalDate currentDate = LocalDate.now();
        Month currentMonth = currentDate.getMonth();
        int currentYear = currentDate.getYear();

        // 获取上个月的月份
        int previousMonthValue = currentMonth.getValue() - 1;
        int previousYear = currentYear;

        if (previousMonthValue == 0) {
            previousMonthValue = 12;
            previousYear--;
        }
        return previousMonthValue;

    }

    public static Integer getTheSameMonthNum(){
        LocalDate currentDate = LocalDate.now();
        Month currentMonth = currentDate.getMonth();
        int currentMonthValue = currentMonth.getValue();
        return currentMonthValue;
    }

    public static String getYearMonthNum(){
        LocalDate currentDate = LocalDate.now();
        int currentYear = currentDate.getYear();

        Month currentMonth = currentDate.getMonth();

        String formattedDate = currentYear + "年" + currentMonth.getValue() + "月";
        return formattedDate;
    }

    /**
     * 当前月yyyyMM
     * @return
     */
    public static String getYearMonthNumY(){
        LocalDate currentDate = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMM");
        String formattedDate = currentDate.format(formatter);
        return formattedDate;
    }
    /**
     * 上月yyyyMM
     * @return
     */
    public static String getLastYearMonthNum(String s){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate currentDate = LocalDate.parse(s, formatter);

        LocalDate lastMonth = currentDate.minusMonths(1);
        DateTimeFormatter formatterYY = DateTimeFormatter.ofPattern("yyyyMM");
        String formattedDate = lastMonth.format(formatterYY);
        System.out.println(formattedDate);
        return formattedDate;
    }

    public static String getTransition(float hours){

        int wholeDays = (int) hours / 6;
        float remainingHours = hours % 6;

        float half = 3.5f;

        int v = (int) remainingHours / (int) half;

        float i = remainingHours % half;

        float v1 = v * 0.5f;

        int i1 = (int) i;
        int i2 = (int) ((i - i1) * 60);
        float v2 = wholeDays + v1;
        String s = v2 + "天" + i1 + "小时" + i2 + "分钟";
        System.out.println(v2 + "天" + i1 + "小时" + i2 + "分钟");
        return s;
    }

    public static Float getTransitionSix(float hours){

        int wholeDays = (int) hours / 8;
        float remainingHours = hours % 8;

        float half = 4.0f;

        float v = remainingHours / half;

        float i = remainingHours % half;

        //float v1 = (int)v * 0.5f;

        int i1 = (int) i;

        //int i2 = (int) ((i - i1) * 60);

        float v2 = wholeDays * 6;
        float v3 = (int) v * 3.5f;

        if (hours ==8){
            return v2;
        }else if (hours <8 && hours >4){
            return v3 + i;
        }else if (hours == 4){
            return v3;
        }else {
            return i;
        }
    }
    public static String sameTimeStr(String s){
        String replace = s.replace("-", "");
        String substring = replace.substring(0, s.length() - 4);
        return substring;
    }
    public static void main(String[] args) {

        Float transitionSix = getTransitionSix(8f);

        System.out.println(transitionSix);

    }
}
